﻿using System.Collections.Generic;

namespace Common
{
    /// <summary>
    /// Name - UserManualSection.cs
    /// Description - 
    /// Programmer - CSC440 Team One
    /// Date coded - 11/27/2011
    /// Arguments - IBusinessService
    /// Variables - None
    /// Files accessed by this code artifact - None
    /// Files changed by this code artifact - None
    /// I/O - None
    /// Error Handling - None
    /// Modifications -
    ///     1.0 - Initial coding
    /// Known Faults -
    ///     1.0 - None known
    /// </summary>
    public class UserManualSection
    {
        private Dictionary<int, UserManualContent> _Content;

        private Dictionary<int, UserManualSection> _Children;

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        /// <value>
        /// The id.
        /// </value>
        public int Id { get; set; }

        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        /// <value>
        /// The title.
        /// </value>
        public string Title { get; set; }

        /// <summary>
        /// Gets or sets the parent.
        /// </summary>
        /// <value>
        /// The parent.
        /// </value>
        public int Parent { get; set; }

        /// <summary>
        /// Gets the content.
        /// </summary>
        public IEnumerable<UserManualContent> Content
        {
            get
            {
                return _Content.Values;
            }
        }

        /// <summary>
        /// Gets the children.
        /// </summary>
        public IEnumerable<UserManualSection> Children
        {
            get
            {
                return _Children.Values;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UserManualSection"/> class.
        /// </summary>
        public UserManualSection()
        {
            _Content = new Dictionary<int, UserManualContent>();

            _Children = new Dictionary<int, UserManualSection>();
        }

        /// <summary>
        /// Adds the content.
        /// </summary>
        /// <param name="userManualContent">Content of the user manual.</param>
        public void AddContent(UserManualContent userManualContent)
        {
            var key = userManualContent.Id;

            if (!_Content.ContainsKey(key))
            {
                _Content.Add(key, userManualContent);
            }
        }

        /// <summary>
        /// Adds the child.
        /// </summary>
        /// <param name="userManualSection">The user manual section.</param>
        public void AddChild(UserManualSection userManualSection)
        {
            var key = userManualSection.Id;

            if (!_Children.ContainsKey(key))
            {
                _Children.Add(key, userManualSection);
            }
        }

        /// <summary>
        /// Finds the user manual content by id.
        /// </summary>
        /// <param name="intUserManualContentId">The int user manual content id.</param>
        /// <returns></returns>
        public UserManualContent FindUserManualContentById(int intUserManualContentId)
        {
            if (_Content.ContainsKey(intUserManualContentId))
            {
                return _Content[intUserManualContentId];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Adds the new content.
        /// </summary>
        /// <returns></returns>
        public UserManualContent AddNewContent()
        {
            var key = -1;

            while (_Content.ContainsKey(key))
            {
                key--;
            }

            var newUserManualContent = new UserManualContent()
            {
                Id = key,
                OrderIndex = _Content.Count + 1,
                UserManualSectionId = Id
            };

            _Content.Add(key, newUserManualContent);

            return newUserManualContent;
        }
    }
}
